//
//  GetData.m
//  EducationAdminClient
//
//  Created by kwsdzjx on 2016/11/7.
//  Copyright © 2016年 周佳兴. All rights reserved.
//

#import "GetData.h"
#import "EducationAdminClient_URL.h"
#import "ThingsModel.h"
#define STYLE0 @"0"
#define STYLE1 @"1"
@implementation GetData


-(void)getDocumentTopViewData:(void (^)(NSMutableArray *, NSMutableArray *, NSMutableArray *))cb{
    
    _cb = cb;
    _menuArr = [[NSMutableArray alloc]init];
    _secondArr = [[NSMutableArray alloc]init];
    _dataArr = [[NSMutableArray alloc]init];
    
    NSString *filename=getSaveFile(userName(),LOGINFOUNCTION);   //获取路径
    NSString *path=[filename stringByAppendingPathComponent:LOGINOK];
    
    NSData *dataNow = [NSData dataWithContentsOfFile:path];
    NSDictionary * dic;
    if(dataNow){
        
        dic = [NSJSONSerialization JSONObjectWithData:dataNow options:NSJSONReadingMutableContainers error:nil];  //读取数据
    }
    //    NSLog(@"%@",dic);
    [self menuTakeTopViewDataUse:dic];
}

-(void)menuTakeTopViewDataUse:(NSDictionary *)_dic2{
    //#elif Student
    // 学员端下情况下 head 需要数据
    NSMutableArray *Menuarr  =[_dic2 objectForKey:@"menuList"];
    NSMutableArray *Menuarr1  =[_dic2 objectForKey:@"menuList"];
    
    for(NSDictionary *dic in Menuarr){
        NSString *str =[NSString stringWithFormat:@"%@", [dic objectForKey:@"parent_menuId"]];
        BOOL is = YES; // 是没有 问题的 是否是父级
        
        for (NSDictionary *dic1 in Menuarr1) {
            NSString *str1 = [NSString stringWithFormat:@"%@", [dic1 objectForKey:@"menuId"]];
            
            if ([str isEqualToString:str1]) {
                
                is = NO;
                
                continue;
            }
        }
        
        if (is) { // 是否是父级
            
            
            [_menuArr addObject:dic];
            //                break;
        }else{
            [_secondArr addObject:dic];
        }
    }
//    NSDictionary *lastDic = @{@"img":@"椭圆-1-拷贝-2"};
//    [_menuArr addObject:lastDic];
    NSLog(@"%lu",(unsigned long)_menuArr.count);
}


#pragma mark - 取出数据源
-(void)getDocumentData:(void (^)(NSMutableArray *, NSMutableArray *, NSMutableArray *))cb{
    
    _cb = cb;
    _menuArr = [[NSMutableArray alloc]init];
    _secondArr = [[NSMutableArray alloc]init];
    _dataArr = [[NSMutableArray alloc]init];
    
    NSString *filename=getSaveFile(userName(),LOGINFOUNCTION);   //获取路径
    NSString *path=[filename stringByAppendingPathComponent:LOGINOK];
    
    NSData *dataNow = [NSData dataWithContentsOfFile:path];
    NSDictionary * dic;
    if(dataNow){
        
        dic = [NSJSONSerialization JSONObjectWithData:dataNow options:NSJSONReadingMutableContainers error:nil];  //读取数据
    }
    //    NSLog(@"%@",dic);
    
    [self menuTakeDataUse:dic];
    
}
#pragma mark - 取出通配的缓存数据
-(void)menuTakeDataUse:(NSDictionary *)_dic2{
    
    
    NSMutableArray *arr  =[_dic2 objectForKey:@"roleFollowList"];
    [self.dataArr removeAllObjects];
    //[self.collectionView.header endRefreshing];
    for (NSDictionary *dic in arr) {
        ThingsModel *model = [[ThingsModel alloc]init];
        model = [model initWithDictionary:dic error:nil];
        [self.dataArr addObject:model];
    }
    
    //#ifdef Main
    if([getUIStyle() isEqualToString:STYLE0]){
        
    }else{
        //#elif Student
        // 学员端下情况下 head 需要数据
        NSMutableArray *Menuarr  =[_dic2 objectForKey:@"menuList"];
        NSMutableArray *Menuarr1  =[_dic2 objectForKey:@"menuList"];
        
        for(NSDictionary *dic in Menuarr){
            NSString *str =[NSString stringWithFormat:@"%@", [dic objectForKey:@"parent_menuId"]];
            BOOL is = YES; // 是没有 问题的 是否是父级
            
            for (NSDictionary *dic1 in Menuarr1) {
                NSString *str1 = [NSString stringWithFormat:@"%@", [dic1 objectForKey:@"menuId"]];
                
                if ([str isEqualToString:str1]) {
                    
                    is = NO;
                    
                    continue;
                }
            }
            
            if (is) { // 是否是父级
                
                
                [_menuArr addObject:dic];
                //                break;
            }else{
                [_secondArr addObject:dic];
            }
        }
//        NSDictionary *lastDic = @{@"img":@"椭圆-1-拷贝-2"};
//        [_menuArr addObject:lastDic];
        NSLog(@"%lu",(unsigned long)_menuArr.count);
    }
    
    _cb(_menuArr, _secondArr, _dataArr);
    
    // #endif
    //[self.collectionView reloadData];
}

@end
